package org.jeecg.common.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.jeecg.common.entity.SysDepart;
import org.jeecg.common.entity.SysDepartTreeModel;
import org.jeecg.common.entity.vo.SysDepartVO;
import org.springframework.data.repository.query.Param;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门 Mapper 接口
 * <p>
 *
 * @Author: Steve
 * @Since：   2019-01-22
 */
public interface SysDepartMapper extends BaseMapper<SysDepart> {

	/**
	 * 根据用户ID查询部门集合
	 */
	public List<SysDepart> queryUserDeparts(@Param("userId") String userId);

	/**
	 * 根据用户名查询部门
	 *
	 * @param username
	 * @return
	 */
	public List<SysDepart> queryDepartsByUsername(@Param("username") String username);

	@Select("select id from sys_depart where org_code=#{orgCode}")
	public String queryDepartIdByOrgCode(@Param("orgCode") String orgCode);

	@Select("select id,parent_id from sys_depart where id=#{departId}")
	public SysDepart getParentDepartId(@Param("departId") String departId);

	/**
	 * 优化查询速度，一次性查出所有机构是否存在叶子节点
	 * @param parentId
	 * @return
	 */
	@Select("SELECT  parent_id ,count(*) as count FROM sys_depart where del_flag ='0' AND parent_id in(select id from sys_depart where parent_id = #{parentId}) GROUP BY parent_id")
	public List<HashMap<String,String>> queryTreeMapByPid(@Param("parentId")String parentId);

	@Select("SELECT  parent_id ,count(*) as count FROM sys_depart where del_flag ='0' AND parent_id in(select id from sys_depart where parent_id = #{parentId} or id = #{parentId})  GROUP BY parent_id")
	public List<HashMap<String,String>> queryTreeMapByPidWithSelf(@Param("parentId")String parentId);
	/**
	 *  根据部门Id查询,当前和下级所有部门IDS
	 * @param departId
	 * @return
	 */
	List<String> getSubDepIdsByDepId(@Param("departId") String departId);

	/**
	 * 根据部门编码获取部门下所有IDS
	 * @param orgCodes
	 * @return
	 */
	List<String> getSubDepIdsByOrgCodes(@org.apache.ibatis.annotations.Param("orgCodes") String[] orgCodes);

	/**
	 * 根据id下级部门数量
	 * @param parentId
	 * @return
	 */
	@Select("SELECT count(*) FROM sys_depart where del_flag ='0' AND parent_id = #{parentId,jdbcType=VARCHAR}")
    Integer queryCountByPid(@Param("parentId")String parentId);
	/**
	 * 根据OrgCod查询所属公司信息
	 * @param orgCode
	 * @return
	 */
	SysDepart queryCompByOrgCode(@Param("orgCode")String orgCode);
	/**
	 * 根据id下级部门
	 * @param parentId
	 * @return
	 */
	@Select("SELECT * FROM sys_depart where del_flag ='0' AND parent_id = #{parentId,jdbcType=VARCHAR}")
	List<SysDepart> queryDeptByPid(@Param("parentId")String parentId);


	/**
	 * 根据数据权限部门和父部门查询
	 * @param departIds
	 * @param parentId
	 * @return
	 */
	List<SysDepartTreeModel> queryTreeListByPid(@org.apache.ibatis.annotations.Param("departIds") String departIds, @org.apache.ibatis.annotations.Param("parentId") String parentId);

	/**
	 * 查询所有org_category为02的单位或03的部门
	 * @return
	 * 调试的时候选有下属单位的id:
	 * and parent_id='07355921da1449bcb7e06906f2d61b60'
	 */
	@Select("SELECT * FROM sys_depart where org_category =#{type,jdbcType=VARCHAR} and del_flag ='0'")
	List<SysDepart> queryAllOrg(String type);


	SysDepartVO queryVOById(String id);

	boolean deleteByLogic(@org.apache.ibatis.annotations.Param("ids") List<String> ids);

	@Select("select parent_id from sys_depart where del_flag = '0' GROUP BY parent_id")
	List<String> queryAllParentGroup();

	@Select("select parent_org_code,count(1) from sys_depart where del_flag = '0' and parent_id = #{id,jdbcType=VARCHAR} GROUP BY LENGTH(parent_org_code)")
	List<Map<String,String>> querySameParentIdNotSameParentOrgCode(String id);
	@Select("select * from sys_depart where del_flag = '0' and org_code in (select org_code FROM sys_depart WHERE del_flag = 0 GROUP BY org_code HAVING COUNT(org_code) > 1) order by org_code")
	List<SysDepart> queryOrgCodeRepeatDepart();
}
